package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import net.qhzw.vip.qiyu.entity.Information;
import net.qhzw.vip.qiyu.entity.Shop;
import net.qhzw.vip.qiyu.entity.Withapply;
import net.qhzw.vip.qiyu.feign.ShopFeignClient;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.AdminUserService;
import net.qhzw.vip.qiyu.service.InformationService;
import net.qhzw.vip.qiyu.service.ShopAccountService;
import net.qhzw.vip.qiyu.service.WithapplyService;
import net.qhzw.vip.qiyu.util.PageParam;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/withapply")
@Api(tags = "提现申请")
public class WithapplyController {

    @Autowired
    private WithapplyService withapplyService;

    @Autowired
    private ShopAccountService shopAccountService;

    @Autowired
    private ShopFeignClient shopFeignClient;

    @Autowired
    private InformationService informationService;


    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/page")
    public JsonResult<IPage<Withapply>> page(PageParam<Withapply> page, Withapply withapply) {
        Long shopId = adminUserService.getLoginUser(SecurityUtils.getUserId()).getShopId();
        withapply.setShopId(shopId);
        IPage<Withapply> pageInfo = withapplyService.findPage(page, withapply);
        return JsonResult.ok(pageInfo);
    }


    /**
     * 提现申请
     *
     * @return
     */
    @PostMapping("/with")
    public JsonResult with(@RequestBody Withapply withapply) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Information information = informationService.selectByShopId(shopId);
        double balance = information.getCapital();
        if (withapply.getAmount() > balance) {
            throw new ApiException("提现金额超出！");
        }
        Shop data = shopFeignClient.findById(shopId).getData();
        withapply.setShopName(data.getShopName());
        withapply.setShopId(shopId);
        withapply.setStatus(0);
        withapplyService.save(withapply);
        return JsonResult.ok();
    }

    /**
     * 提现审核
     *
     * @return
     */
    @PostMapping("/examine")
    public JsonResult examine(@RequestBody Withapply withapply) {
//        Withapply dbWithapply = withapplyService.getById(withapply.getId());
//        Integer status = withapply.getStatus();
//        if (status == 1) {
//            ShopAccount shopAccount = new ShopAccount();
//            shopAccount.setTransferAmount(dbWithapply.getAmount());
//            shopAccount.setTransactionType(0);
//            shopAccount.setShopId(dbWithapply.getShopId());
//            shopAccount.setRemark("提现");
//            double balance = shopAccountService.getBalance(dbWithapply.getShopId());
//            //shopAccount.setBalance(Arith.sub(balance, dbWithapply.getAmount()));
//            Shop data = shopFeignClient.findById(dbWithapply.getShopId()).getData();
//
//            shopAccount.setShopName(data.getShopName());
//            shopAccountService.save(shopAccount);
//        }


        Withapply dbWithapply = withapplyService.getById(withapply.getId());
        Long shopId = dbWithapply.getShopId();
        Information information = informationService.selectByShopId(shopId);
        Double capital = information.getCapital();
        if (dbWithapply.getAmount() > capital) {
            withapply.setStatus(2);
            withapply.setMsg("提现金额超出！");
            withapplyService.updateById(withapply);
            throw new ApiException("提现金额超出！");
        }

        withapplyService.updateById(withapply);
        return JsonResult.ok();
    }


    /**
     * 打款
     *
     * @return
     */
    @PostMapping("/payment")
    public JsonResult payment(@RequestBody Withapply withapply) {
        Withapply dbWithapply = withapplyService.getById(withapply.getId());
        if (1 != dbWithapply.getStatus()) {
            throw new ApiException("审核未通过！");
        }
        withapply.setShopId(dbWithapply.getShopId());
        withapply.setAmount(dbWithapply.getAmount());
        withapplyService.payment(withapply);
        return JsonResult.ok();
    }


}
